package com.fin.zw.aiqas.controller;

import com.alibaba.fastjson.JSONObject;
import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.TaskInfo;
import com.fin.zw.aiqas.model.component.Drill;
import com.fin.zw.aiqas.model.request.CqTaskRequest;
import com.fin.zw.aiqas.model.request.TaskInfoRequest;
import com.fin.zw.aiqas.model.response.ResultResponse;
import com.fin.zw.aiqas.model.response.TaskInfoResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.QcTaskInfoService;
import com.fin.zw.aiqas.service.TaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Api(value = "任务管理", description = "任务管理")
@RequestMapping("/task")
public class TaskInfoController {

    private final static Logger logger = LoggerFactory.getLogger(TaskInfoController.class);

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private QcTaskInfoService qcTaskInfoService;

    /**
     * 增加
     *
     * @param request
     * @param sessionModel
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加任务信息", notes = "增加任务信息")
    public ResponseEntity<String> add(@RequestBody TaskInfoRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel) {
            taskInfoService.add(request, sessionModel);
            return ResponseEntity.ok().body("增加成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除任务信息", notes = "删除任务信息")
    public ResponseEntity<String> delete(@PathVariable Long id) {
            taskInfoService.delete(id);
            return ResponseEntity.ok().body("删除成功");
    }

    /**
     * 修改任务信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "修改任务信息", notes = "修改任务信息")
    public ResponseEntity<String> update(@RequestBody TaskInfoRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel) {
            taskInfoService.update(request, sessionModel);
            return ResponseEntity.ok().body("修改成功");
    }

    @GetMapping("/fetchPage")
    @ApiOperation(value = "分页查新任务信息", notes = "分页查新任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "第几页(0...N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页显示几条数据"),
            @ApiImplicitParam(name = "sort", dataType = "string", paramType = "query", allowMultiple = true, value = "排序,属性名，asc|desc"),
    })
    public ResponseEntity<Page<TaskInfo>> fetchPage(TaskInfoRequest request, Pageable pageable,
                                                    @ApiIgnore @CurrentUser SessionModel sessionModel) {
        Page<TaskInfo> pageList = taskInfoService.fetchPage(request, pageable, sessionModel);
            return ResponseEntity.ok().body(pageList);
    }

    @GetMapping("/fetch/{id}")
    @ApiOperation(value = "根据ID查询任务详情", notes = "根据ID查询任务详情")
    public ResponseEntity<TaskInfoResponse> queryById(@PathVariable Long id) {
        TaskInfoResponse response = taskInfoService.queryById(id);
        return ResponseEntity.ok().body(response);
    }


    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改任务状态", notes = "修改任务状态")
    public ResponseEntity<Void> updateStatus(@RequestBody TaskInfoRequest request) {
        taskInfoService.updateStatus(request.getId());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/executeTaskById")
    @ApiOperation(value = "定时任务注册(单一的任务)", notes = "定时任务注册")
    public ResponseEntity<Void> executeTaskById(@RequestParam("id") Long id) throws Exception {
        taskInfoService.executeTask(id);
        return ResponseEntity.ok().build();
    }


    @RequestMapping(value = "/qcDetail", method = {RequestMethod.POST, RequestMethod.PATCH})
    @ApiOperation(value = "句子预测结果回调", notes = "句子预测结果回调")
    public ResponseEntity<ResultResponse> qcDetail(@RequestBody Drill request) {
        logger.info("句子开始质检：" + JSONObject.toJSONString(request));
        ResultResponse response = new ResultResponse();
        response.setMgs("句子质检成功");
        qcTaskInfoService.qcDetail(request, response);
        return ResponseEntity.ok().body(response);
    }

    @RequestMapping(value = "/updateRunStatus", method = {RequestMethod.POST, RequestMethod.PATCH})
    public ResponseEntity<ResultResponse> updateRunStatus(@RequestBody CqTaskRequest request) {
        ResultResponse response = new ResultResponse();
        response.setMgs("更新成功");
        qcTaskInfoService.updateRunStatus(request, response);
        return ResponseEntity.ok().body(response);
    }


    @GetMapping("/fetch")
    @ApiOperation(value = "查询全部任务", notes = "查询全部任务")
    public ResponseEntity<List<TaskInfo>> fetchAll() {
        List<TaskInfo> taskInfos = taskInfoService.fetchAll();
        return ResponseEntity.ok().body(taskInfos);
    }
}
